package com.hrt.thirtyparty.sms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

/**
 * 
 * @author Administrator
 *
 */
public class SMSModualHandlerImpl implements SMSModualHandler {

	
	//protected String regex="[\[A-Za-z\]\]";
	
	protected String regex="\\[(.*?)\\]";
	
	
	
	/***
	 * 
	 */
	@Override
	public String genarateSMSContent(Integer smsBusType,
			Map<String, Object> params, String modalContent) {
		
		String smsContent = null;
		Pattern patt = Pattern.compile(regex);
//	    String str = "你好，的风格的[sasd]电饭锅gh[sasd][sasd][sasd]jgjg,,,f电饭锅[dgdsf]的风格ghgfg地方地方[fdsf]";
	    Matcher mat = patt.matcher(modalContent);
	    List<String> parmsList = new ArrayList<String>();
	    while (mat.find()) {
	    	parmsList.add(mat.group(0));
	    	System.out.println(mat.group(0));
	    }
		
				//
		if (smsBusType != null && smsBusType != -1) {

			if (smsBusType.intValue() == SMSBusinessType.MEMBER_REGSITER_SMS.ordinal()) {
				
				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
							String paramName=p.getKey();
							String paramValue=p.getValue().toString();
							paramName="["+paramName+"]";
							if(parmsList.contains(paramName)){
								modalContent=modalContent.replace(paramName, paramValue);
							}
							
					}
					smsContent=modalContent;
				}
					
				return smsContent;
				
				
			} else if (smsBusType.intValue() == SMSBusinessType.MEMBER_FIND_PASSWORD
					.ordinal()) {

				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
						String paramName=p.getKey();
						String paramValue=p.getValue().toString();
						paramName="["+paramName+"]";
						if(parmsList.contains(paramName)){
							modalContent=modalContent.replace(paramName, paramValue);
						}
						
					}
					smsContent=modalContent;
				}
				return smsContent;

			} else if (smsBusType.intValue() == SMSBusinessType.MEMBER_SEND_ORDER
					.ordinal()) {

				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
						String paramName=p.getKey();
						String paramValue=p.getValue().toString();
						paramName="["+paramName+"]";
						if(parmsList.contains(paramName)){
							modalContent=modalContent.replace(paramName, paramValue);
						}
						
					}
					smsContent=modalContent;
				}
				
				return smsContent;
				
			} else if (smsBusType.intValue() == SMSBusinessType.ORDER_PAY
					.ordinal()) {

				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
						String paramName=p.getKey();
						String paramValue=p.getValue().toString();
						paramName="["+paramName+"]";
						if(parmsList.contains(paramName)){
							modalContent=modalContent.replace(paramName, paramValue);
						}
					}
					smsContent=modalContent;
				}
				
				return smsContent;
				
			} else if (smsBusType.intValue() == SMSBusinessType.ORDER_SEND.ordinal()) {

				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
						String paramName=p.getKey();
						String paramValue=p.getValue().toString();
						paramName="["+paramName+"]";
						if(parmsList.contains(paramName)){
							modalContent=modalContent.replace(paramName, paramValue);
						}
						
						smsContent=modalContent;	
					}
				}
				return smsContent;
				
			} else if (smsBusType.intValue() == SMSBusinessType.ORDER_ROLLBACK.ordinal()) {
				if (params != null && !params.isEmpty()) {
					for (Iterator<Entry<String, Object>> itr = params
							.entrySet().iterator(); itr.hasNext();) {
						Map.Entry<String, Object> p = itr.next();
						String paramName=p.getKey();
						String paramValue=p.getValue().toString();
						paramName="["+paramName+"]";
						if(parmsList.contains(paramName)){
							modalContent=modalContent.replace(paramName, paramValue);
						}
					}
				}
				return smsContent;
			}
		}
		return null;
		}
	
	
	
	
	public static void main(String[] args){
		
	SMSModualHandler handler=new SMSModualHandlerImpl();
	Map map=new HashMap();
	map.put("loginName", "windy");
	map.put("vaildCode", "123456");
	String modalContent="【何人堂创意产品网】恩人[loginName]，你好！你此次修改密码的手机验证码为[vaildCode],感谢你对何人堂创意产品网的支持，请继续关注http://www.who158.com";

	String smsContent=handler.genarateSMSContent(SMSBusinessType.MEMBER_REGSITER_SMS.ordinal(), map, modalContent);
	System.out.println(smsContent);
	}
}
